package com.curricula.ejemplo;

import org.xmldb.api.base.*;
import org.xmldb.api.modules.*;
import org.xmldb.api.*;

public class QueryExample {

	public static void main(String args[]) throws Exception {
		String driver = "org.exist.xmldb.DatabaseImpl"; // Driver
		Class<?> cl = Class.forName(driver);// Cargar el driver
		Database database = (Database) cl.newInstance(); // Instancia de la BD
		database.setProperty("create-database", "true");
		DatabaseManager.registerDatabase(database); // Registrar la BD
		// Accedemos a la coleccion
		Collection col = DatabaseManager.getCollection(
				"xmldb:exist://localhost:8082/exist/xmlrpc/db/prueba", "admin","toor");
		XPathQueryService service = (XPathQueryService) col.getService(
				"XPathQueryService", "1.0");
		service.setProperty("pretty", "true");
		service.setProperty("encoding", "ISO-8859-1");
		// Consulta a lanzar
		//ResourceSet result = service.query("for $b in doc('libros.xml')//apellido return $b");
		ResourceSet result = service.query("for $b in doc('libros.xml')//libro let $c := $b//autor where count($c) > 2 order by $b/titulo return $b/ titulo");
		ResourceIterator i = result.getIterator();
		while (i.hasMoreResources()) { // Procesamos el resultado
			Resource r = i.nextResource();
			System.out.println((String) r.getContent());
		}
	}
}
